﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Data.SqlClient;
using System.Drawing;


namespace myScheduler
{
        class modelScheduler
        {
            List<string> ColorList;

            SQLcontrol SQLcontroller = new SQLcontrol();

            public bool authenticateUser(string username, string password)
            {
                MD5 crypto_Service = new MD5CryptoServiceProvider();
                string hashedPassword = BitConverter.ToString(crypto_Service.ComputeHash(ASCIIEncoding.Default.GetBytes(password)));

                DataTable SQLdata = new DataTable();
                string sql = SQLcontroller.ReadSQLscript("authenticateUser.sql");
                sql = sql.Replace("$USER", username);
                sql = sql.Replace("$PASS", hashedPassword);
                SQLdata = SQLcontroller.SQLread(sql).Tables[0];
                if (SQLdata.Rows.Count > 0)
                {
                    return true; //An user was found with matching username/password
                }
                else
                {
                    return false; //No user was found with matching username/password
                }
            }

            public List<combo> generate(string courses)
            {
                //Break the string to a list
                //courses = "ELEC 311";
                List<string> courseList = courses.Split(new char[] {','}).ToList();
                //Process courses and return list of viable schedule
                return WebScraper.generate(courseList);
            }

            public bool classExist(string courseCode)
            {
                DataSet reader = new DataSet();
                string sql = "SELECT * FROM Course_Object3 WHERE course_code + ' ' + course_number = '" + courseCode + "'";
                DataTable SQLdata; // = new DataTable();
                SQLdata = SQLcontroller.SQLread(sql).Tables[0];
                if (SQLdata.Rows.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public int fetchDayRow(daysWeek dayWeekInput)
            {
                switch(dayWeekInput)
                {
                    case daysWeek.sunday:
                    {
                        return 1;                        
                    }
                    case daysWeek.monday:
                    {
                        return 2;                        
                    }
                    case daysWeek.tuesday:
                    {
                        return 3;                        
                    }
                    case daysWeek.wednesday:
                    {
                        return 4;                        
                    }
                    case daysWeek.thursday:
                    {
                        return 5;                        
                    }
                    case daysWeek.friday:
                    {
                        return 6;                        
                    }
                    case daysWeek.saturday:
                    {
                        return 7;                        
                    }
                    default:
                    {
                        return 0;                        
                    }
                }
            }

            public string fetchDayName(int dayId)
            {
                switch (dayId)
                {
                    case 0:
                        return "Sunday";                        
                    case 1:
                        return "Monday";                        
                    case 2:
                        return "Tuesday";                        
                    case 3:
                        return "Wednesday";                        
                    case 4:
                        return "Thursday";                        
                    case 5:
                        return "Friday";                        
                    case 6:
                        return "Saturday";
                    default:
                        return "-1";
                }
            }

            public string convertIntToTime(double dblTime)
            {
                string tempdblTime = dblTime.ToString();    //Convert to string
                if(tempdblTime.Length < 3)                  //If there the number has no decimal then add decimal (such as 6PM: 18 => 18.0)
                    tempdblTime += ".0";
                string[] tempArrayTime = tempdblTime.Split('.');    //Split at the period
                string hour = tempArrayTime[0];                     //Get hour string
                double dblMinute = (double.Parse(tempArrayTime[1])/100) * 60;   //Calculate equivalent for minutes
                string minute = dblMinute.ToString();               //Get minute string
                if (minute.Length < 2)                      //If the lenght is 1, then add a zero (6:30PM: 18:3 => 18:30)
                    minute += "0";
                return hour + ":" + minute;                         //return in format HH:MM
            }

            public string getColor(int colorId)
            {
                if (colorId == 0)
                    generateColorList();
                colorId = colorId % ColorList.Count; //insure that it is never out of index
                return ColorList[colorId];
            }

            private void generateColorList()
            {
                ColorList = new List<string>();
                ColorList.Add("Red");
                ColorList.Add("Lime");
                ColorList.Add("Aqua");
                ColorList.Add("Coral");
                ColorList.Add("DarkOrange");
                ColorList.Add("Fuchsia");
                ColorList.Add("LightCoral");
                ColorList.Add("Yellow");
                ColorList.Add("YellowGreen");
                ColorList.Add("Salmon");
            }

            public void saveSchedule(string courseList, string username)
            {
                string sql = SQLcontroller.ReadSQLscript("saveSchedule.sql");
                sql = sql.Replace("$SCHEDULE", courseList);
                sql = sql.Replace("$USERNAME", username);
                SQLcontroller.SQLwrite(sql);
            }

            public string reloadSchedule(string username)
            {
                DataTable SQLdata = new DataTable();
                string sql = SQLcontroller.ReadSQLscript("reloadSchedule.sql");
                sql = sql.Replace("$USERNAME", username);
                SQLdata = SQLcontroller.SQLread(sql).Tables[0];
                if (SQLdata.Rows.Count > 0)
                {
                    string tempData = "";
                    foreach (DataRow drRow in SQLdata.Rows)
                    {
                        tempData = drRow[1].ToString();
                    }
                    return tempData;
                }
                else
                {
                    return "";
                }
            }
        }     
}